﻿
@{
    ViewBag.Title = "Index";
}

<div class="jumbotron">
    <p><a href="#" onclick="payFac()" class="btn btn-primary btn-lg">JSAPI支付 &raquo;</a></p>

    <p><a href="#" onclick="payh5()" class="btn btn-primary btn-lg">H5支付 &raquo;</a></p>
</div>



<script>


    ///微信H5支付开始

    function payh5() {
        $.post("/Home/payH5", function (d) {
            console.log(d);
            if (d.length == 0) {
                alert("下单失败");
                return;
            }
            var ss = $.parseJSON(d);
            if (ss.result_code == "SUCCESS") {
                location.href = ss.mweb_url;
                return;
            }

        });
    }





    ///微信支付JS API 支付流程一下

    function payFac() {
        $.post("/Home/pay", function (d) {
            console.log(d);
            if (d != undefined && d.length > 0) {
                callpay(d);
            }
        })
    }
    /* 微信支付调用h5页面 */
    function callpay(wxJsApiParam) {

        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            }
            else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        }
        else {
            jsApiCall(wxJsApiParam);
        }
    }

    //调用微信JS api 支付
    function jsApiCall(wxJsApiParam) {
        var obj = eval("(" + wxJsApiParam + ")");
        alert(wxJsApiParam);
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            obj,//josn串
            function (res) {
                alert(JSON.stringify(res));
                //业务处理代码
                if (res.errMsg != undefined) {
                    alert(res.errMsg);
                    return;
                }
                alert(res.err_msg);
                switch (res.err_msg) {
                    case "get_brand_wcpay_request:ok":  //支付成功
                        alert("支付成功");
                        break;
                    case "get_brand_wcpay_request:fail":    //支付失败
                        alert("支付失败");
                        break;
                    case "get_brand_wcpay_request:cancel":    //取消支付
                        alert("取消支付");
                        break;
                }

            });
    }
</script>
